package com.ruoyi.basic.service.impl;

import java.util.List;
import javax.validation.Validator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basic.mapper.PlayerMapper;
import com.ruoyi.basic.domain.Player;
import com.ruoyi.basic.service.IPlayerService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanValidators;

/**
 * 播放器配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-13
 */
@Service
public class PlayerServiceImpl implements IPlayerService {
    @Autowired
    private PlayerMapper playerMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询播放器配置
     *
     * @param playerId 播放器配置主键
     * @return 播放器配置
     */
    @Override
    public Player selectPlayerById(Long playerId) {
        return playerMapper.selectPlayerById(playerId);
    }

    /**
     * 根据播放器标识查询播放器配置
     *
     * @param code 播放器标识
     * @return 播放器配置
     */
    @Override
    public Player selectPlayerByCode(String code) {
        return playerMapper.selectPlayerByCode(code);
    }

    /**
     * 查询播放器配置列表
     *
     * @param player 播放器配置
     * @return 播放器配置
     */
    @Override
    public List<Player> selectPlayerList(Player player) {
        return playerMapper.selectPlayerList(player);
    }

    /**
     * 查询启用的播放器配置列表
     *
     * @return 播放器配置集合
     */
    @Override
    public List<Player> selectActivePlayerList() {
        return playerMapper.selectActivePlayerList();
    }

    /**
     * 新增播放器配置
     *
     * @param player 播放器配置
     * @return 结果
     */
    @Override
    public int insertPlayer(Player player) {
        return playerMapper.insertPlayer(player);
    }

    /**
     * 修改播放器配置
     *
     * @param player 播放器配置
     * @return 结果
     */
    @Override
    public int updatePlayer(Player player) {
        return playerMapper.updatePlayer(player);
    }

    /**
     * 批量删除播放器配置
     *
     * @param playerIds 需要删除的播放器配置主键
     * @return 结果
     */
    @Override
    public int deletePlayerByIds(Long[] playerIds) {
        return playerMapper.deletePlayerByIds(playerIds);
    }

    /**
     * 删除播放器配置信息
     *
     * @param playerId 播放器配置主键
     * @return 结果
     */
    @Override
    public int deletePlayerById(Long playerId) {
        return playerMapper.deletePlayerById(playerId);
    }

    /**
     * 校验播放器标识是否唯一
     *
     * @param player 播放器配置
     * @return 结果
     */
    @Override
    public String checkCodeUnique(Player player) {
        Long playerId = StringUtils.isNull(player.getPlayerId()) ? -1L : player.getPlayerId();
        Player info = playerMapper.checkCodeUnique(player.getCode());
        if (StringUtils.isNotNull(info) && info.getPlayerId().longValue() != playerId.longValue()) {
            return "1"; // 不唯一
        }
        return "0"; // 唯一
    }

    /**
     * 导入播放器配置数据
     *
     * @param playerList 播放器配置列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importPlayer(List<Player> playerList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(playerList) || playerList.size() == 0) {
            throw new ServiceException("导入播放器配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Player player : playerList) {
            try {
                // 验证是否存在这个播放器(根据code查询)
                Player p = playerMapper.selectPlayerByCode(player.getCode());
                if (StringUtils.isNull(p)) {
                    // 不存在，新增
                    BeanValidators.validateWithException(validator, player);
                    player.setCreateBy(operName);
                    // 设置默认值
                    if (StringUtils.isEmpty(player.getStatus())) {
                        player.setStatus("1");
                    }
                    if (StringUtils.isNull(player.getSort())) {
                        player.setSort(100);
                    }
                    if (StringUtils.isEmpty(player.getNeedParse())) {
                        player.setNeedParse("0");
                    }
                    if (StringUtils.isEmpty(player.getTarget())) {
                        player.setTarget("_self");
                    }
                    playerMapper.insertPlayer(player);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、播放器 " + player.getCode() + " 导入成功");
                } else if (isUpdateSupport) {
                    // 存在且支持更新
                    BeanValidators.validateWithException(validator, player);
                    player.setPlayerId(p.getPlayerId());
                    player.setUpdateBy(operName);
                    playerMapper.updatePlayer(player);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、播放器 " + player.getCode() + " 更新成功");
                } else {
                    // 存在但不更新
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、播放器 " + player.getCode() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、播放器 " + player.getCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
